let fs = require("fs");  
let path = require("path");  
  
// 递归创建目录 异步方法  
function mkdirs(dirname, callback) {
    fs.exists(dirname, function (exists) {
        if (exists) {
            callback(false);
        } else {
            mkdirs(path.dirname(dirname), function () {
                fs.mkdir(dirname, callback);
            });
        }
    });
}
// 递归创建目录 同步方法
function mkdirsSync(dirname) {
    if (fs.existsSync(dirname)) {
        return true;
    } else {
        if (mkdirsSync(path.dirname(dirname))) {
            fs.mkdirSync(dirname);
            return true;
        }
    }
    return false;
}
// 递归删除目录 同步方法
function rmdirSync(dirname) {
    if( fs.existsSync(dirname) ) {
        if (dirname.charAt(dirname.length-1)=="/") {
            dirname = dirname.substring(0, dirname.length-1);
        }
        let files = fs.readdirSync(dirname);
        files.forEach(function(file, index) {
            let curPath = `${dirname}/${file}`;
            if(fs.statSync(curPath).isDirectory()) { // delete folder
                rmdirSync(curPath);
            } else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(dirname);
        return true;
    }
    return false;
}

exports.mkdirs = mkdirs;
exports.mkdirsSync = mkdirsSync;
exports.rmdirSync = rmdirSync;